/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package comunication;

import GUI.StringTokenizer;
import service.*;
import commonpackage.*;
import java.util.Date;
import java.util.Vector;

/**
 *
 * @author Rente
 */
public class talkToServer {

    String[] ret = new String[2];
    GetMyStuffService_Stub service;

    public talkToServer() {

        service = new GetMyStuffService_Stub();
    }

    public String[] getAllPOIS() {

        //return ss.getAllPOIs();
        return ret;
    }

    public Vector getPOIsByKW(String key) {
        try {
            String[] arr = service.getPOIsByKW(key);
            return buildSimplePOIs(arr, false);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public Vector getPOIsNear(String key) {
        try {
            String[] arr = service.getNearLocation(key);
            System.out.println("NEAR: " + arr.length);
            return buildSimplePOIs(arr, false);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public Vector getPOIsGPS(String lat, String longi) {
        try {
            String[] arr = service.getNearCoord(lat, longi);
            System.out.println("GPS: " + arr.length);
            return buildSimplePOIs(arr, true);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private POI buildMyPOI(String string) {
        POI poi;
        StringTokenizer st;
        if (string != null) {

            poi = new POI();
            st = new StringTokenizer(string, "#");
            System.out.println("TOKS: " + st.countTokens());
            string = st.nextToken();
            System.out.println(string);
            poi.setId(Integer.parseInt(string));
            string = st.nextToken();
            System.out.println(string);
            poi.setName(string);
            string = st.nextToken();
            System.out.println(string);
            poi.setType(string);
            string = st.nextToken();
            System.out.println(string);
            if (string.equals("null")) {
                poi.setDescription("N/A");
            } else {
                poi.setDescription(string);
            }
            string = st.nextToken();
            System.out.println(string);
            if (string.equals("null")) {
                poi.setDescription("N/A");
            } else {
                poi.setAddress(string);
            }
            string = st.nextToken();
            System.out.println(string);
            if (string.equals("null")) {
                poi.setDescription("N/A");
            } else {
                poi.setTelephone(string);
            }
            string = st.nextToken();
            System.out.println(string);
            poi.setTicket(string.equals("true"));
            String url = st.nextToken();
            if (url.startsWith("http")) {
                poi.setUrl(url);
            }
            return poi;

        }
        return null;
    }

    private Vector buildSimplePOIs(String[] arr, boolean prox) {
        //String type, descr,  address,  telephone, boolean order, url
        if (arr.length > 0) {
            Vector v = new Vector();
            POI poi;
            StringTokenizer st;
            for (int i = 0; i < arr.length; i++) {
                String string = arr[i];
                System.out.println("FULL: " + string);
                if (string != null) {
                    poi = new POI();
                    st = new StringTokenizer(string, "#");
                    System.out.println("TOKS: " + st.countTokens());
                    string = st.nextToken();
                    System.out.println(string);
                    poi.setId(Integer.parseInt(string));
                    string = st.nextToken();
                    System.out.println(string);
                    poi.setName(string);
                    string = st.nextToken();
                    System.out.println(string);
                    poi.setType(string);
                    string = st.nextToken();
                    System.out.println(string);
                    if (string.equals("null")) {
                        poi.setDescription("N/A");
                    } else {
                        poi.setDescription(string);
                    }
                    string = st.nextToken();
                    System.out.println(string);
                    if (string.equals("null")) {
                        poi.setDescription("N/A");
                    } else {
                        poi.setAddress(string);
                    }
                    string = st.nextToken();
                    System.out.println(string);
                    if (string.equals("null")) {
                        poi.setDescription("N/A");
                    } else {
                        poi.setTelephone(string);
                    }
                    string = st.nextToken();
                    System.out.println(string);
                    poi.setTicket(string.equals("true"));
                    String url = st.nextToken();
                    if (url.startsWith("http")) {
                        poi.setUrl(url);
                    }
                    if (prox) {
                        while (st.hasMoreTokens()) {
                            string = st.nextToken();
                        }
                        System.out.println(string);
                        poi.setDist(string);
                    }
                    if (!v.contains(poi)) {
                        v.addElement(poi);
                    }
                }
            }
            return v;
        } else {
            return null;
        }
    }

    public POI getPOIbyID(int id) {
        try {
            String str = service.getPOIbyID(id);
            return buildMyPOI(str);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public Vector getEatingByKW(String key) {
        try {
            String[] arr = service.getEatingByKW(key);
            System.out.println("Eating: " + arr.length);
            return buildSimplePOIs(arr, false);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public Vector getHostingByKW(String key) {
        try {
            String[] arr = service.getHostingByKW(key);
            System.out.println("Hosting: " + arr.length);
            return buildSimplePOIs(arr, false);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public Vector getTravelByKW(String key) {
        try {
            String[] arr = service.getTravelByKW(key);
            System.out.println("Travel: " + arr.length);
            return buildSimplePOIs(arr, false);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public Vector getOrdersByMobNumber(int number) {

        try {
            String[] arr = service.getOrdersByMobNumber(number + "");
            System.out.println("Orders: " + arr.length);
            return buildOrders(arr);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public boolean makeOrder(String poiID, String phone, String name, String numPeople, Date resDate, String type) {
        System.out.println("DATA: " + resDate);
        String[] arr = {poiID, phone, name, numPeople, resDate.getTime() + "", type};
        try {
            service.makeOrder(arr);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    private Vector buildOrders(String[] arr) {
        System.out.println("BUILD ORDERS");
        if (arr.length > 0) {
            Vector v = new Vector();
            Order order;
            StringTokenizer st;
            for (int i = 0; i < arr.length; i++) {
                String string = arr[i];
                //System.out.println(string);
                if (string != null) {
                    order = new Order();
                    st = new StringTokenizer(string, "#");
                    string = st.nextToken();
                    System.out.println(string);
                    order.setPoiid(Integer.parseInt(string));
                    string = st.nextToken();
                    System.out.println(string);
                    order.setTelNumber(string);
                    string = st.nextToken();
                    System.out.println(string);
                    order.setNumPeople(string);
                    string = st.nextToken();
                    System.out.println(string);
                    order.setNameOrderer(string);
                    string = st.nextToken();
                    System.out.println(string);
                    order.setDateMade(new Date(Long.parseLong(string)));
                    System.out.println("ORder date made: " + order.getDateMade());
                    string = st.nextToken();
                    System.out.println(string);
                    order.setResDate(new Date(Long.parseLong(string)));
                    System.out.println("ORder date RES: " + order.getResDate());
                    string = st.nextToken();
                    System.out.println(string);
                    order.setPoiName(string);
                    if (!v.contains(order)) {
                        v.addElement(order);
                    }
                }
            }
            return v;
        } else {
            return null;
        }
    }
}
